package com.cmcc.passenger.ui.order.viewmodel

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.databinding.ObservableField
import com.cmcc.passenger.R
import com.cmcc.passenger.data.entity.OrderRecordItem
import com.cmcc.shared.SdkManager
import java.text.SimpleDateFormat
import java.util.*

class MyOrderItemViewModel(context: Context, model: OrderRecordItem) {

    private val mContext = context
    val orderType = ObservableField<String>(convertType(model.orderRecord.type))
    val origin = ObservableField<String>(model.orderRecord.startAddress)
    val destination = ObservableField<String>(model.orderRecord.endAddress)
    val createTime = ObservableField<String>(convertDate(model.orderRecord.createTime ?: ""))
    val status = ObservableField<String>(getOrderDesc(model.orderRecord.status))
    @RequiresApi(Build.VERSION_CODES.M)
    val statusColor = ObservableField<Int>(getOrderColor(model.orderRecord.status))
    val statusAlpha = ObservableField<Double>(getOrderAlpha(model.orderRecord.status))


    private fun convertType(type:Int):String{
        return if(type == 1) mContext.getString(R.string.passenger_use_car_real_time) else mContext.getString(R.string.passenger_use_car_reservation)
    }

    private fun convertDate(date: String): String {
        val parse = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        var format = SimpleDateFormat("yyyy年MM月dd HH时mm分", Locale.getDefault())
        when (SdkManager.getLanguage()) {
            SdkManager.LANGUAGE_ENG ->
                format = SimpleDateFormat("yyyy-MM-dd HH-mm", Locale.getDefault())
            SdkManager.LANGUAGE_ZH_CN ->
                format = SimpleDateFormat("yyyy年MM月dd HH时mm分", Locale.getDefault())
            SdkManager.LANGUAGE_ZH_TW ->
                format = SimpleDateFormat("yyyy年MM月dd HH時mm分", Locale.getDefault())
        }
        val formatDate = parse.parse(date)
        return format.format(formatDate)
    }

    private fun getStatusDesc(status: Int?): String {
        return when (status) {
            1 -> mContext.getString(R.string.passenger_driver_come)
            2 -> mContext.getString(R.string.passenger_driver_go)
            3, 4, 5 -> mContext.getString(R.string.passenger_order_success)
            6 -> mContext.getString(R.string.order_complete_title)
            else -> mContext.getString(R.string.order_be_refuse_title)
        }
    }

    private fun getOrderDesc(status: Int?):String{
        return when (status) {
            1,2,3, 4, 5 -> mContext.getString(R.string.passenger_order_in_progress)
            6 -> mContext.getString(R.string.passenger_order_over)
            7 -> mContext.getString(R.string.passenger_order_cancel)
            else -> mContext.getString(R.string.passenger_order_not_in_time)
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    private fun getOrderColor(status: Int?):Int{
        return when (status) {
            1,2,3, 4, 5 -> mContext.getColor(R.color.passenger_colorAccents)
            6, 7 -> mContext.getColor(R.color.passenger_text_gray)
            else -> mContext.getColor(R.color.passenger_dark_green)
        }
    }

    private fun getOrderAlpha(status: Int?):Double{
        return when (status) {
            1,2,3, 4, 5 -> 1.0
            6, 7 -> 0.6
            else -> 1.0
        }
    }

}